package flink;


import jakarta.validation.constraints.AssertTrue;
import org.hibernate.validator.constraints.Length;
import java.util.*;
import java.util.Arrays;

public class test {
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Boolean getAdmin() {
        return isAdmin;
    }

    public void setAdmin(Boolean admin) {
        isAdmin = admin;
    }

    @Length(min = 2,max = 6,message = "长度不和规范")
    private String name;

    @AssertTrue(message = "123")
    private Boolean isAdmin;
    private static final double dampingFactor = 0.85;
    private static final double epsilon = 0.0001;

    public static void main(String[] args) {
        // 构建邻接矩阵
        int[][] edges = {{1, 2}, {1, 3}, {2, 3}, {3, 1}};
        int n = 3;
        double[][] matrix = new double[n][n];
        for (int[] edge : edges) {
            matrix[edge[0] - 1][edge[1] - 1] = 1.0;
        }

        // 初始化 PageRank 值
        double[] pageRank = new double[n];
        Arrays.fill(pageRank, 1.0 / n);

        // 迭代计算
        double[] oldPageRank = new double[n];
        while (true) {
            System.arraycopy(pageRank, 0, oldPageRank, 0, n);

            for (int i = 0; i < n; ++i) {
                double sum = 0.0;
                for (int j = 0; j < n; ++j) {
                    if (matrix[j][i] > 0) {
                        sum += oldPageRank[j] / outDegree(j, matrix);
                    }
                }
                pageRank[i] = (1 - dampingFactor) / n + dampingFactor * sum;
            }

            if (isConverged(pageRank, oldPageRank)) {
                break;
            }
        }

        // 输出计算结果
        for (int i = 0; i < n; ++i) {
            System.out.printf("PageRank of node %d is %.4f\n", i + 1, pageRank[i]);
        }
    }

    private static double outDegree(int i, double[][] matrix) {
        double sum = 0.0;
        for (int j = 0; j < matrix[i].length; ++j) {
            sum += matrix[i][j];
        }
        return sum;
    }

    private static boolean isConverged(double[] a, double[] b) {
        for (int i = 0; i < a.length; ++i) {
            if (Math.abs(a[i] - b[i]) > epsilon) {
                return false;
            }
        }
        return true;
    }
}